package com.wenwu.service.impl;

import java.sql.SQLException;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.wenwu.common.bean.Role;
import com.wenwu.common.bean.RoleResources;
import com.wenwu.common.exception.RelicsException;
import com.wenwu.common.page.PaginationVO;
import com.wenwu.dao.RoleMapper;
import com.wenwu.dao.RoleResourcesMapper;
import com.wenwu.service.RoleService;

/**
 * @project ATHome-Manager-Service
 * @class RoleServiceImpl.java
 * @author zhujiagui E-mail:<zhujiagui@zkingsoft.com>
 * @date 2015年3月3日 下午5:49:12
 * @description 角色模块service层实现类
 */
@Service
public class RoleServiceImpl implements RoleService {
	@Autowired
	private RoleMapper roleMapper;
	@Autowired
	private RoleResourcesMapper roleResourcesMapper;

	public int selectTotalRecord(Role role, PaginationVO pageVo) throws RelicsException {
		try {
			return roleMapper.selectTotalRecord(role, pageVo);
		} catch (SQLException e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public List<Role> selectList(Role role, PaginationVO pageVo) throws RelicsException {
		try {
			return roleMapper.selectList(role, pageVo);
		} catch (SQLException e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public int addRole(Role role) throws RelicsException {
		try {
			return roleMapper.insertSelective(role);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public int updateRole(Role role) throws RelicsException {
		try {
			return roleMapper.updateByPrimaryKey(role);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public Role selectByKey(Integer roleId) throws RelicsException {
		try {
			return roleMapper.selectByPrimaryKey(roleId);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public int deleteRoleByKey(List<?> list) throws RelicsException {
		try {
			return roleMapper.deleteRoleByKey(list);
		} catch (Exception e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public int saveBatchByRoleId(List<RoleResources> list, Integer roleId)
			throws RelicsException {
		try {// 先删除原来分配的资源在分配资源
			roleResourcesMapper.deleteBatchByRoleId(roleId);
			return roleResourcesMapper.saveBatch(list);
		} catch (SQLException e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}

	public List<RoleResources> selectCurrentList(Integer roleId)
			throws RelicsException {
		try {
			return roleResourcesMapper.selectCurrentList(roleId);
		} catch (SQLException e) {
			throw new RelicsException("系统错误，请稍后再试", e);
		}
	}
}
